import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:family_app/src/features/gallery/models/photo_model.dart';

/// 照片网格项
class PhotoGridItem extends StatelessWidget {
  final PhotoModel photo;
  final VoidCallback onTap;
  final VoidCallback onDelete;
  final bool isSelectable; // 是否处于选择模式
  final bool isSelected; // 是否被选中
  final VoidCallback onSelect; // 选择回调

  const PhotoGridItem({
    Key? key,
    required this.photo,
    required this.onTap,
    required this.onDelete,
    this.isSelectable = false,
    this.isSelected = false,
    required this.onSelect,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: isSelectable ? onSelect : onTap, // 选择模式下点击触发选择，否则查看照片
      onLongPress: onSelect, // 长按触发选择
      child: Stack(
        children: [
          // 照片
          Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8.r),
              border: Border.all(
                color: isSelected ? Colors.blue : Colors.grey[300]!, // 选中状态下边框变蓝
                width: isSelected ? 2.0 : 1.0, // 选中状态下边框加粗
              ),
            ),
            clipBehavior: Clip.antiAlias,
            child: _buildImage(),
          ),
          // 删除按钮 - 仅在非选择模式下显示
          // if (!isSelectable)
          //   Positioned(
          //     top: 4.r,
          //     right: 4.r,
          //     child: GestureDetector(
          //       onTap: onDelete,
          //       child: Container(
          //         padding: EdgeInsets.all(4.r),
          //         decoration: BoxDecoration(
          //           color: Colors.black.withOpacity(0.6),
          //           shape: BoxShape.circle,
          //         ),
          //         child: Icon(
          //           Icons.delete,
          //           size: 16.r,
          //           color: Colors.white,
          //         ),
          //       ),
          //     ),
          //   ),
          // 选择框 - 仅在选择模式下显示
          if (isSelectable)
            Positioned(
              bottom: 4.r,
              right: 4.r,
              child: Container(
                padding: EdgeInsets.all(4.r),
                decoration: BoxDecoration(
                  color: isSelected ? Colors.blue : Colors.black.withOpacity(0.6),
                  shape: BoxShape.circle,
                ),
                child: Icon(
                  isSelected ? Icons.check : Icons.check_box_outline_blank,
                  size: 16.r,
                  color: Colors.white,
                ),
              ),
            ),
          // 上传状态指示
        //   if (photo.uploadStatus != UploadStatus.uploaded)
        //     Positioned(
        //       bottom: 4.r,
        //       right: 4.r,
        //       child: Container(
        //         padding: EdgeInsets.symmetric(horizontal: 6.r, vertical: 2.r),
        //         decoration: BoxDecoration(
        //           color: _getStatusColor(),
        //           borderRadius: BorderRadius.circular(4.r),
        //         ),
        //         child: Text(
        //           _getStatusText(),
        //           style: TextStyle(
        //             color: Colors.white,
        //             fontSize: 10.sp,
        //           ),
        //         ),
        //       ),
        //     ),
        ],
      ),
    );
  }

  /// 构建照片图片
  Widget _buildImage() {
    if (photo.localPath != null && photo.localPath!.isNotEmpty) {
      // 本地图片
      return Image.file(
        File(photo.localPath!),
        fit: BoxFit.cover,
        width: double.infinity,
        height: double.infinity,
        errorBuilder: (context, error, stackTrace) => _buildErrorPlaceholder(),
      );
    } else if (photo.url != null && photo.url!.isNotEmpty) {
      // 网络图片
      final imageUrl =
          (photo.thumbnailUrl != null && photo.thumbnailUrl!.isNotEmpty)
              ? photo.thumbnailUrl!
              : photo.url!;

      return Image.network(
        imageUrl,
        fit: BoxFit.cover,
        width: double.infinity,
        height: double.infinity,
        errorBuilder: (context, error, stackTrace) => _buildErrorPlaceholder(),
      );
    } else {
      return _buildErrorPlaceholder();
    }
  }

  /// 构建错误占位图
  Widget _buildErrorPlaceholder() {
    return Container(
      color: Colors.grey[200],
      child: Center(
        child: Icon(
          Icons.broken_image,
          size: 24.r,
          color: Colors.grey[400],
        ),
      ),
    );
  }

  /// 获取上传状态文本
  String _getStatusText() {
    if (photo.uploadStatus == null) {
      return '未上传';
    }

    switch (photo.uploadStatus!) {
      case UploadStatus.pending:
        return '未上传';
      case UploadStatus.uploading:
        return '上传中';
      case UploadStatus.failed:
        return '上传失败';
      case UploadStatus.uploaded:
        return '已上传';
    }
  }

  /// 获取上传状态颜色
  Color _getStatusColor() {
    if (photo.uploadStatus == null) {
      return Colors.orange;
    }

    switch (photo.uploadStatus!) {
      case UploadStatus.pending:
        return Colors.orange;
      case UploadStatus.uploading:
        return Colors.blue;
      case UploadStatus.failed:
        return Colors.red;
      case UploadStatus.uploaded:
        return Colors.green;
    }
  }
}
